'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskSubtaskCtrl
 * @description
 * # TaskSubtaskCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskSubtaskCtrl', function(DataService, Util, openApi, $routeParams, $location) {
        this.subtaskObj = {}
        this.confirm = function() {
            if (!this.subtaskObj.content) {
                Util.Toast("请输入子任务名")
                return;
            }
            if (!this.subtaskObj.userId) {
                Util.Toast("请选择负责人")
                return;
            }
            if ($routeParams.type == "taskDetail") {
                this.addSubTask();
            } else {
                this.addSubTaskLocal()
            }
        };
        this.addSubTask = function() {
            var param = {};
            param = this.subtaskObj;
            param.refId = this.param.pkId
            DataService.getData(openApi.addSubTask, param).then(function(data) {
                this.subtaskObj.pkId = data
                this.subtaskObj.modifyFlag = 2
                this.addSubTaskLocal()
            }.bind(this));
        };
        this.addSubTaskLocal = function() {
            this.param.subTasks.push(this.subtaskObj);
            Util.clearLocalInfo("subtaskObj")
            this.saveInfo();
            Util.goBack();
        };
        this.goaddjoinpeople = function() {
            Util.saveLocalInfo("subtaskObj", this.subtaskObj)
            $location.path("task/addjoinpeople/subtask")
        };
        this.saveInfo = function() {
            var params = this.param;
            // $routeParams.id通过id确定唯一回显id
            Util.savaParamForReview(params, "TaskNewtaskCtrl");
        };
        this.init = function() {
            this.param = Util.getParamForReview("TaskNewtaskCtrl")
            this.subtaskObj = Util.getLocalInfo("subtaskObj");
            this.param.subTasks = this.param.subTasks || [];
        };
        this.init();
    });
